package com.cl.edu.service.admin;

import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.NewsDAO;
import com.cl.edu.dao.NewssortDAO;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Newssort;

@Service
@Transactional
public class NewsManager {
	@Autowired
	private NewsDAO newsDAO;
	@Autowired
	private NewssortDAO newssortDAO;

	/**
	 * 获取所有资讯栏目
	 *  
	 * @return
	 */
	public List<Newssort> getNewssorts() {
		return newssortDAO.getAll();
	} 

	/**
	 * 分页查询新闻资讯
	 * 
	 * @param page
	 * @return
	 */
	public Page<News> getNewsList(Page<News> page) {
		StringBuffer hql = new StringBuffer("from News model ");
		hql.append(" where 1=1 ");
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("title".equals(filter.getPropertyName())) {
				hql.append(" and model.title like '%")
						.append(filter.getPropertyValue()).append("%'");
			} else if ("typeid".equals(filter.getPropertyName())) {
				hql.append(" and model.channel.id =").append(filter.getPropertyValue());
			} else if ("content".equals(filter.getPropertyName())) {
				String contentValue = "";
				if(!"请输入关键字：".equals(filter.getPropertyValue())){
					contentValue = filter.getPropertyValue();
				}
						
				hql.append(" and model.content like '%")
				.append(contentValue).append("%'");
			}

		}
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = newsDAO.findPage(page, hql.toString());
		return page;
	}

	/**
	 * 按id查询新闻详细
	 * @param id
	 * @return
	 */
	public News getNewsById(Long id) {
//		News news = (News)newsDAO.getSession().get(News.class, id);
//		if(!Hibernate.isInitialized(news.getChannel())){
//			Hibernate.initialize(news.getChannel());
//		}
//		return (News)newsDAO.getSession().get(News.class, id);
		return newsDAO.get(id);
	}

	/**
	 * 新增或更新新闻
	 * @param entity
	 */
	public void saveOrUpdateNews(News entity) {
		newsDAO.saveOrUpdate(entity);
	}
	
	public void clear() {
		newsDAO.getSession().clear();
	}


	/**
	 * 查找栏目
	 * @param newssortId
	 * @return
	 */
	public Newssort getNewssortById(Long newssortId) {
		return newssortDAO.get(newssortId);
	}

	/**
	 * 删除新闻
	 * @param id
	 */
	public void deleteNews(Long id) {
		newsDAO.delete(id);
	}
	
	public News preContent(Page<News> page, Long id, long parentId)	{
		page.setOrder(Page.ASC);
		return uniContent(page, id, ">", parentId);
	}

	public News nextContent(Page<News> page, Long id, long parentId)	{
		page.setOrder(Page.DESC);
		return uniContent(page, id, "<", parentId);
	}
	
	private News uniContent(Page<News> page ,Long id, String mark, long parentId)	{
		try {
			StringBuffer hql = new StringBuffer("from News model ");
			hql.append(" where 1=1 ");
			hql.append(" and model.id ").append(mark).append(id);
			hql.append(" and model.channel.id = ").append(parentId);
			hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
			page = newsDAO.findPage(page, hql.toString());

			if (page.getResult().size() != 0)
				return page.getResult().get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
